<template>
  <Container class="rebate-checks-list">
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <TabPane label="门店积分奖励" name="tab1">
        <homeTable
          :list="list"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
        />
      </TabPane>
    </Tabs>
    <ChangePointModelItem ref="changePoint" @onComplete="getData"/>
  </Container>
</template>
<script>
import PointRewardData from './list/PointRewardData'
import HeadFormItem from './list/HeadFormItem'
import ChangePointModelItem from './list/ChangePointModelItem'
import homeTable from '@/components/homeTable'
import { clearEmpty } from '@/util'
export default {
  components: {
    HeadFormItem,
    ChangePointModelItem,
    homeTable
  },
  data() {
    return {
      tab: "tab1",
      list: PointRewardData
    }
  },
  mounted() {
    this.getData()
    this.$bus.$on('onReward', ({row}) => {this.onReward(row)})
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    onSearchBtnClick(form) {
      this.list.currentPage = 1;
      this.list.form = form
      this.getData();
    },
    getData() {
      this.list.status = "loading";
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        storeName: this.list.form.storeName,
        storeTypeId: this.list.form.storeTypeId,
        areaId: this.list.form.areaId ? this.list.form.areaId : null,
        cityId: this.list.form.cityId ? this.list.form.cityId : null,
        provinceId: this.list.form.provinceId ? this.list.form.provinceId : null,
        distributorId: this.list.form.distributorId,
        storeId: this.list.form.storeId
      };
      param = clearEmpty(param)
      this.$ajaxGet("/assets/reward_integral/page", param).then(data => {
        this.list.data = []
        this.list.totalItem = Number(data.total)
        this.list.data = (data.records || []).map(item => {
          return {
            id: item.id,
            storeId: item.storeId,
            storeName: item.storeName,
            prevAddress: (item.provinceName ? item.provinceName : '') + (item.cityName ? item.cityName : '') + (item.areaName ? item.areaName : ''),
            address: item.address,
            ownerName: item.ownerName,
            storePhone: item.storePhone,
            distributorName: item.distributorName,
            storeTypeName: item.storeTypeName
          }
        })
        this.list.status = "pass"
      }).catch(() => {
        this.list.status = "error"
      })
    },
    onReward(row) {
      this.$refs.changePoint.updataForm(row)
    },
    onPageChange(number) {
      this.list.currentPage = number
      this.getData()
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1
      this.list.pageSize = pageSize
      this.getData()
    }
  },
  watch: {
  }
};
</script>
